package com.ljkj.cordial.chat.activity;


import android.app.Activity;
import android.content.Intent;
import android.text.InputType;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.lanjiu.lib.imclient.dto.user.RegisteredUserDto;
import com.lanjiu.lib.imclient.ims.IMSContacts;
import com.lanjiu.lib.utils.crypto.DES;
import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.ui.AppEventActivity;
import com.ljkj.cordial.chat.ui.views.RegisterView;
import com.ljkj.cordial.chat.util.ButtonSubmitStateUtil;
import com.ljkj.cordial.chat.widget.RectButtonHintDialog;
import com.nic.view.widget.ContainsEmojiEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册
 */
public class RegisterActivity extends AppEventActivity<RegisterView> implements RegisterView.RegisterCallback {

    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_password)
    ContainsEmojiEditText tvPassword;
    @BindView(R.id.cb_isOpen)
    CheckBox cbIsOpen;
    @BindView(R.id.tv_password2)
    ContainsEmojiEditText tvPassword2;
    @BindView(R.id.cb_isOpen2)
    CheckBox cbIsOpen2;
    @BindView(R.id.tv_nickname)
    ContainsEmojiEditText tvNickname;
    @BindView(R.id.tv_phone)
    ContainsEmojiEditText tvPhone;
    @BindView(R.id.tv_register)
    TextView tvRegister;

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        getToolbar().setNavigationIcon(R.mipmap.tab_ic_back_close);
        cbIsOpen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    tvPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        cbIsOpen2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvPassword2.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    tvPassword2.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        new ButtonSubmitStateUtil(tvRegister, tvPassword, tvPassword2, tvNickname) {
            @Override
            public boolean checkState() {
                return check();
            }
        };
    }

    private boolean check() {
        return !TextUtils.isEmpty(tvPassword.getText()) && !TextUtils.isEmpty(tvPassword2.getText())
                && tvPassword.getText().length() >= 6 && tvPassword2.getText().length() >= 6
                && tvPassword.getText().toString().equals(tvPassword2.getText().toString())
                && !TextUtils.isEmpty(tvNickname.getText());
    }

    public static void jumpTo(Activity activity) {
        Intent intent = new Intent(activity, RegisterActivity.class);
        activity.startActivityForResult(intent, Canstants.REQUEST_CODE_BACK);
    }

    @OnClick(R.id.tv_register)
    public void onViewClicked() {
        if (!check()) {
            return;
        }
        String password = DES.encode(tvPassword.getText().toString());
        obtainView().doRegister(password, tvNickname.getText().toString(), tvPhone.getText().toString());
    }

    @Override
    protected RegisterView bindNettyMessageView() {
        return new RegisterView(this);
    }

    @Override
    public void onRegisterCallback(int code, String msg, RegisteredUserDto data) {
        if (IMSContacts.ResponseCode.SUCCESS == code && data != null) {
            if (!TextUtils.isEmpty(data.getUserId())) {
//                SharedUtils.put(this, Canstants.KEY_UserId, data.getUserId());
//                SharedUtils.put(this, Canstants.KEY_UseType, UserTableBean.TYPE_REGISTER);
                UserTableBean user = new UserTableBean();
                String password = DES.encode(tvPassword.getText().toString());
                user.setSecret(password);
                user.insert(data);
                RegisterSuccessActivity.jumpTo(this);
                setResult(Canstants.REQUEST_CODE_BACK);
                finish();
            }

        } else {
            new RectButtonHintDialog(this)
                    .setTitle("注册失败")
                    .setContent("网络繁忙or本地请求服务器失败")
                    .setSubmit("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                        }
                    }).show();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Canstants.REQUEST_CODE_BACK && resultCode == Canstants.REQUEST_CODE_BACK) {
            setResult(Canstants.REQUEST_CODE_BACK);
            finish();
        }
    }
}
